<script setup lang="ts">
import { reactive, ref } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import AddFill from "@iconify-icons/ri/add-circle-line";
import { useBackup } from "./hook";
import { NewSystemBackup, RestoreSystemBackup } from "@/api/systemcofig";
import { message } from "@/utils/message";
defineOptions({
  name: "Databackup"
});
const tableRef = ref();
const {
  loading,
  columns,
  dataList,
  onSearch,
  pagination,
  selectedNum,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  onSelectionCancel,
  onbatchDel,
  restreS
} = useBackup(tableRef);
const showDialog = ref(false);
const showrestore = ref(false);
const form = reactive({
  objName: ""
});

const addform = ref();
const restorForm = ref();
const confirm = async () => {
  const res = await NewSystemBackup(form);
  if (res.isSuccess == true) {
    message("新增备份成功", { type: "success" });
    addform.value.resetFields();
    onSearch();
  }
  showDialog.value = false;
};
const confirmrestore = async () => {
  const res = await RestoreSystemBackup(form);
  if (res.isSuccess == true) {
    message("还原备份成功", { type: "success" });
    restorForm.value.resetFields();
    onSearch();
  }
  showrestore.value = false;
};
</script>

<template>
  <div class="w-[calc(100%-200px)]">
    <PureTableBar title="数据备份" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="showrestore = true"
        >
          还原备份
        </el-button>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="showDialog = true"
        >
          新建备份
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <div
          v-if="selectedNum > 0"
          v-motion-fade
          class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
        >
          <div class="flex-auto">
            <span
              style="font-size: var(--el-font-size-base)"
              class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
            >
              已选 {{ selectedNum }} 项
            </span>
            <el-button type="primary" text @click="onSelectionCancel">
              取消选择
            </el-button>
          </div>
          <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
            <template #reference>
              <el-button type="danger" text class="mr-1"> 批量删除 </el-button>
            </template>
          </el-popconfirm>
        </div>
        <pure-table
          row-key="id"
          ref="tableRef"
          adaptive
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :default-sort="{ prop: 'id', order: 'descending' }"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        />
      </template>
    </PureTableBar>
    <el-dialog v-model="showDialog" title="新建备份">
      <div class="addressWrapper">
        <el-form :model="form" label-width="120px" ref="addform">
          <el-form-item label="备份描述" prop="objName">
            <el-input v-model="form.objName" type="textarea" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="confirm">确定</el-button>
            <el-button @click="showDialog = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

    <el-dialog v-model="showrestore" title="还原备份">
      <div class="addressWrapper">
        <el-form :model="restreS" label-width="120px" ref="restorForm">
          <el-form-item label="编号" prop="rsID">
            <el-input v-model="restreS.rsID" type="text" disabled />
          </el-form-item>
          <el-form-item label="备份名称" prop="path">
            <el-input v-model="restreS.path" type="textarea" disabled />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="confirmrestore">确定</el-button>
            <el-button @click="showrestore = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-button:focus-visible) {
  outline: none;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
